package com.edu.exam.entity.examination.paper.technical;

import jakarta.persistence.*;
import lombok.Data;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

// 导入外部枚举类
import com.edu.exam.enums.examination.paper.SegmentedImageFormat;
import com.edu.exam.enums.examination.paper.SegmentedStorageLocation;
import com.edu.exam.enums.examination.paper.SegmentedCompressionFormat;

/**
 * 智能切分图像实体类
 *
 * 根据E-04-04 智能切分子模块设计实现
 * 对应exam_segmented_image数据表
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class SegmentedImage {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 关联的切分任务ID，关联exam_segmentation_task表
     */
    @Column(name = "task_id", nullable = false)
    private Long taskId;

    /**
     * 关联的切分结果ID，关联exam_segmentation_result表
     */
    @Column(name = "result_id", nullable = false)
    private Long resultId;

    /**
     * 图像文件名称
     */
    @Column(name = "image_file_name", nullable = false, length = 255)
    private String imageFileName;

    /**
     * 图像格式：PNG、JPEG、TIFF、BMP
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "image_format", nullable = false, length = 10)
    private SegmentedImageFormat imageFormat;

    /**
     * 图像宽度（像素）
     */
    @Column(name = "image_width")
    private Integer imageWidth;

    /**
     * 图像高度（像素）
     */
    @Column(name = "image_height")
    private Integer imageHeight;

    /**
     * 图像颜色深度（位）
     */
    @Column(name = "color_depth")
    private Integer colorDepth;

    /**
     * 图像分辨率（DPI）
     */
    @Column(name = "resolution_dpi")
    private Integer resolutionDpi;

    /**
     * 图像大小（字节）
     */
    @Column(name = "file_size_bytes")
    private Long fileSizeBytes;

    /**
     * 图像质量分数（0-100）
     */
    @Column(name = "image_quality_score")
    private Double imageQualityScore;

    /**
     * 图像清晰度分数（0-100）
     */
    @Column(name = "clarity_score")
    private Double clarityScore;

    /**
     * 对比度分数（0-100）
     */
    @Column(name = "contrast_score")
    private Double contrastScore;

    /**
     * 亮度分数（0-100）
     */
    @Column(name = "brightness_score")
    private Double brightnessScore;

    /**
     * 噪声水平分数（0-100，越高表示噪声越少）
     */
    @Column(name = "noise_level_score")
    private Double noiseLevelScore;

    /**
     * 存储位置：LOCAL_DISK（本地磁盘）、NETWORK_STORAGE（网络存储）、CLOUD_STORAGE（云存储）、DATABASE（数据库）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "storage_location", nullable = false, length = 20)
    private SegmentedStorageLocation storageLocation;

    /**
     * 存储路径
     */
    @Column(name = "storage_path", length = 500)
    private String storagePath;

    /**
     * 相对路径（用于访问）
     */
    @Column(name = "relative_path", length = 500)
    private String relativePath;

    /**
     * 访问URL
     */
    @Column(name = "access_url", length = 1000)
    private String accessUrl;

    /**
     * 文件哈希值（MD5或SHA256）
     */
    @Column(name = "file_hash", length = 128)
    private String fileHash;

    /**
     * 图像压缩格式：NONE（无压缩）、LOSSLESS（无损压缩）、LOSSY（有损压缩）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "compression_format", length = 20)
    private SegmentedCompressionFormat compressionFormat;

    /**
     * 压缩质量（0-100，仅适用于有损压缩）
     */
    @Column(name = "compression_quality")
    private Integer compressionQuality;

    /**
     * 压缩后大小（字节）
     */
    @Column(name = "compressed_size_bytes")
    private Long compressedSizeBytes;

    /**
     * 压缩率（百分比）
     */
    @Column(name = "compression_ratio")
    private Double compressionRatio;

    /**
     * 是否包含透明通道
     */
    @Column(name = "has_transparency")
    private Boolean hasTransparency;

    /**
     * 透明通道质量分数（0-100，如果有透明通道）
     */
    @Column(name = "transparency_quality_score")
    private Double transparencyQualityScore;

    /**
     * 图像元数据（JSON格式，包含EXIF等信息）
     */
    @Column(name = "image_metadata", length = 2000, columnDefinition = "TEXT")
    private String imageMetadata;

    /**
     * 图像处理历史（JSON格式）
     */
    @Column(name = "processing_history", length = 2000, columnDefinition = "TEXT")
    private String processingHistory;

    /**
     * 缩略图路径
     */
    @Column(name = "thumbnail_path", length = 500)
    private String thumbnailPath;

    /**
     * 缩略图大小（字节）
     */
    @Column(name = "thumbnail_size_bytes")
    private Long thumbnailSizeBytes;

    /**
     * 备份存储路径
     */
    @Column(name = "backup_storage_path", length = 500)
    private String backupStoragePath;

    /**
     * 备份创建时间
     */
    @Column(name = "backup_created_time")
    private LocalDateTime backupCreatedTime;

    /**
     * 访问次数
     */
    @Column(name = "access_count")
    private Long accessCount;

    /**
     * 最后访问时间
     */
    @Column(name = "last_access_time")
    private LocalDateTime lastAccessTime;

    /**
     * 是否为临时文件
     */
    @Column(name = "is_temporary")
    private Boolean isTemporary;

    /**
     * 过期时间（对于临时文件）
     */
    @Column(name = "expiration_time")
    private LocalDateTime expirationTime;

    /**
     * 是否已归档
     */
    @Column(name = "is_archived")
    private Boolean isArchived;

    /**
     * 归档时间
     */
    @Column(name = "archived_time")
    private LocalDateTime archivedTime;

    /**
     * 归档存储路径
     */
    @Column(name = "archive_storage_path", length = 500)
    private String archiveStoragePath;

    /**
     * 数据完整性校验结果
     */
    @Column(name = "integrity_check_passed")
    private Boolean integrityCheckPassed;

    /**
     * 最后校验时间
     */
    @Column(name = "last_integrity_check_time")
    private LocalDateTime lastIntegrityCheckTime;

    /**
     * 扩展属性（JSON格式）
     */
    @Column(name = "additional_attributes", length = 2000, columnDefinition = "TEXT")
    private String additionalAttributes;

    // ================================
    // JPA 关联关系
    // ================================

    /**
     * 关联的切分任务
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id", insertable = false, updatable = false)
    private SegmentationTask segmentationTask;

    /**
     * 关联的切分结果
     */
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "result_id", insertable = false, updatable = false)
    private SegmentationResult segmentationResult;

    // ================================
    // 审计字段
    // ================================

    @CreationTimestamp
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @UpdateTimestamp
    @Column(name = "updated_time")
    private LocalDateTime updatedTime;

    @Version
    @Column(name = "version")
    private Integer version = 0;

    @Column(name = "is_deleted")
    private Boolean isDeleted = false;
}